package com.kld.amaptset.ui.activity;

import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.kld.amaptset.R;
import com.kld.amaptset.model.entity.Meizi;
import com.kld.amaptset.presenter.MainPresenter;
import com.kld.amaptset.ui.IView.MainView;
import com.kld.amaptset.ui.adaper.MeiziRecycViewAdaper;
import com.kld.amaptset.ui.widget.LMRecyclerView;
import com.kld.amaptset.util.SPDataUtil;
import com.kld.amaptset.util.TipUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by KELUNGDE on 2017/8/5.
 */

public class MainActivity extends ToolBarActivity<MainPresenter> implements SwipeRefreshLayout.OnRefreshListener,LMRecyclerView.LoadMoreListener,  MainView{

    protected   String TAG=this.getClass().getSimpleName();
    private MainPresenter mPresenter;
    private List<Meizi> datas;
    private MeiziRecycViewAdaper adaper;
    private int page=1;
    private boolean isRefresh=true;
    private boolean canLoading=true;


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_view)
    LMRecyclerView recyclerView;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    @OnClick(R.id.fab)
    void  fabClick(){
        presenter.fabClick();
    }
    @Override
    protected int getLayoutResID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initPresenter() {
        Log.i(TAG,"initPresenter");
        mPresenter=new MainPresenter(MainActivity.this,this);
        mPresenter.initView();
    }

    @Override
    public void initView() {
        datas= SPDataUtil.getFirstPageGirls(this);
        if (datas==null) datas=new ArrayList<>();
        adaper=new MeiziRecycViewAdaper(MainActivity.this,datas);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLoadMoreListener(this);
        recyclerView.applyFloatingActionButton(fab);
        recyclerView.setAdapter(adaper);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,R.color.colorAccent,R.color.blue);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                mPresenter.fatchMeiziData(page);
            }
        });
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               recyclerView.smoothScrollToPosition(0);
            }
        });
    }
    @Override
    public void showProgress() {
        if (!swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideProgress() {
        if (swipeRefreshLayout.isRefreshing()){
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void showErrorView() {
        isRefresh=true;
        TipUtil.showTipWithAction(fab, "加载出错", "重试", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.fatchMeiziData(page);
            }
        });
    }
    @Override
    public void showNoMoreData() {
        canLoading=false;
        TipUtil.showSnackTip(fab,"没有数据了");
    }
    @Override
    protected boolean canBack() {
        return false;
    }
    @Override
    public void showMeiziList(List<Meizi> meiziList) {
        canLoading=true;
        page++;
        if (isRefresh){
            SPDataUtil.saveFirstPageGirls(this,meiziList);
            datas.clear();
            datas.addAll(meiziList);
            adaper.notifyDataSetChanged();
            isRefresh=false;
        } else {
            datas.addAll(meiziList);
            adaper.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh() {//下拉刷新
        isRefresh=true;
        page=1;
        mPresenter.fatchMeiziData(page);
    }

    @Override
    public void loadMore() {//加载更多
        if (canLoading){
            mPresenter.fatchMeiziData(page);
            canLoading=false;
        }
    }
}
